package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.jdbc.enums.SQLConditionEnum;
import com.gitee.zycra.jdbc.enums.SQLLinkEnum;
import com.gitee.zycra.jdbc.model.PageQueryWrapper;
import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.jdbc.util.SQLBlock;
import com.gitee.zycra.jdbc.util.SQLChain;
import com.gitee.zycra.union.api.common.bo.PageQuery;
import com.gitee.zycra.union.api.common.model.ApiInterface;
import com.gitee.zycra.union.api.common.param.PageQueryInterfaceParam;
import com.gitee.zycra.union.api.common.vo.InterfaceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author zycra
 */
@Repository
public class ApiInterfaceDao extends BaseDao<ApiInterface> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 根据目录ID查询接口数量
     *
     * @param folderId 目录ID
     * @return 接口数量
     */
    public int countByFolderId(Long folderId) {
        return countTableByNotNullColumnEquals(new ApiInterface().setInterfaceFolderId(folderId).setDeleted(false));
    }

    /**
     * 添加接口
     *
     * @param apiInterface 接口对象
     * @return 影响行数
     */
    public int insert(ApiInterface apiInterface) {
        return insertIntoTable(apiInterface);
    }

    /**
     * 更新接口
     *
     * @param apiInterface 接口对象
     * @return 影响行数
     */
    public int updateById(ApiInterface apiInterface) {
        return updateTableById(apiInterface);
    }

    /**
     * 删除接口
     *
     * @param id 接口ID
     * @return 影响行数
     */
    public int updateToDelete(Long id) {
        return updateTableById(new ApiInterface().setId(id).setDeleted(true));
    }

    /**
     * 查询接口
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    public PageResult<InterfaceVO> pageQueryInterface(PageQuery<PageQueryInterfaceParam> pageQuery) {
        String countSql = "SELECT COUNT(ai.id) FROM api_interface_folder aif INNER JOIN api_interface ai ON aif.id = ai.interface_folder_id"
                + " AND aif.deleted = 0 AND ai.deleted = 0";
        String sql = "SELECT aif.folder_name, ai.id, ai.interface_name, ai.interface_method, ai.interface_uri, ai.interface_status,"
                + " ai.interface_folder_id, ai.request_param_type, ai.request_param, ai.response_param, ai.success_response_value,"
                + " ai.fail_response_value FROM api_interface_folder aif INNER JOIN api_interface ai ON aif.id = ai.interface_folder_id"
                + " AND aif.deleted = 0 AND ai.deleted = 0";
        PageQueryInterfaceParam param = pageQuery.getParam();
        SQLChain sqlChain = SQLChain.builder()
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "aif.project_id", SQLConditionEnum.EQUALS, param.getProjectId()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "ai.interface_folder_id", SQLConditionEnum.EQUALS, param.getInterfaceFolderId()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "ai.interface_name", SQLConditionEnum.CONTAINS, param.getInterfaceName()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "ai.interface_uri", SQLConditionEnum.CONTAINS, param.getInterfaceUri()))
                .build();
        return commonSelector.selectForPage(getJdbcTemplate(), InterfaceVO.class,
                new PageQueryWrapper(countSql + sqlChain.getSQL(), sqlChain.getParamList(),
                        sql + sqlChain.getSQL(), sqlChain.getParamList(), pageQuery.getPage(), pageQuery.getSize()));
    }
}
